package com.sudo.service.impl;

import com.github.pagehelper.PageHelper;
import com.sudo.common.utils.DateUtil;
import com.sudo.common.utils.IpAddressUtils;
import com.sudo.common.utils.PagedGridResult;
import com.sudo.mapper.my.SysUserLoginRecordMapper;
import com.sudo.pojo.SysUserLoginRecord;
import com.sudo.service.BaseService;
import com.sudo.service.SysUserLoginRecordService;
import nl.bitwalker.useragentutils.Browser;
import nl.bitwalker.useragentutils.OperatingSystem;
import nl.bitwalker.useragentutils.UserAgent;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author：为道日损 2020-06-19 22:27
 * webSite：https://www.sudo.ren
 */
@Service
public class SysUserLoginRecordServiceImpl extends BaseService implements SysUserLoginRecordService {

    @Autowired
    private SysUserLoginRecordMapper sysUserLoginRecordMapper;


    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    @Override
    public int addUserLoginRecord(String username, HttpServletRequest request) {
        String userAgent = request.getHeader("User-Agent");
        String Ip = IpAddressUtils.getIpAdrress(request);
        String dateTime = DateUtil.dateToStringWithTime();
        SysUserLoginRecord sysUserLoginRecord = new SysUserLoginRecord();
        //转成UserAgent对象
        UserAgent ua = UserAgent.parseUserAgentString(userAgent);
        //浏览器信息
        Browser browser = ua.getBrowser();
        //系统信息
        OperatingSystem os = ua.getOperatingSystem();
        //系统名称
        String systemName = os.getName();
        //设备
        String device = os.getDeviceType().getName();
        //浏览器
        String browserInfo = browser.getName() + "_" + ua.getBrowserVersion().getVersion();
        sysUserLoginRecord.setUsername(username);
        sysUserLoginRecord.setSource(Ip);
        sysUserLoginRecord.setDevice(device);
        sysUserLoginRecord.setSystemName(systemName);
        sysUserLoginRecord.setBrowser(browserInfo);
        sysUserLoginRecord.setLoginTime(dateTime);
        return sysUserLoginRecordMapper.insertSelective(sysUserLoginRecord);
    }

    @Transactional(propagation = Propagation.SUPPORTS,rollbackFor = Exception.class)
    @Override
    public PagedGridResult queryUserLoginRecordByPage(Integer page, Integer pageSize, String username , String startDate , String endDate) {
        PageHelper.startPage(page, pageSize);
        Example example = new Example(SysUserLoginRecord.class);
        example.orderBy("id").desc();
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(username)) {
            criteria.andEqualTo("username",username);
        } else if (StringUtils.isNotBlank(startDate)) {
            criteria.andGreaterThanOrEqualTo("loginTime",startDate);
        } else if (StringUtils.isNotBlank(endDate)) {
            criteria.andLessThanOrEqualTo("loginTime",endDate);
        }
        List<SysUserLoginRecord> sysUserBehaviorList = sysUserLoginRecordMapper.selectByExample(example);
        return setterPagedGrid(sysUserBehaviorList, page);
    }
}
